package com.reggie.module.controller;

import com.reggie.common.ResultResponseVo;
import com.reggie.module.entity.dto.employeeDto.AddEmployeeDto;
import com.reggie.module.entity.dto.employeeDto.ChangeStatusDto;
import com.reggie.module.entity.dto.employeeDto.EmployeeLoginDto;
import com.reggie.module.entity.dto.employeeDto.UpdateEmployeeDto;
import com.reggie.module.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:30:42
 * @Desc:
 */
@RestController
@Slf4j
@RequestMapping("/employee")
@CrossOrigin
@Validated
@Api(tags = "员工模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 登录
     *
     * @param request
     * @param employeeLoginDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public ResultResponseVo login(HttpServletRequest request, @RequestBody @Valid EmployeeLoginDto employeeLoginDto) {
        return employeeService.login(request, employeeLoginDto);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public ResultResponseVo logout(HttpServletRequest request) {
        return employeeService.logout(request);
    }

    /**
     * 添加员工
     *
     * @param request
     * @param addEmployeeDto
     * @return
     */
    @PostMapping
    @ApiOperation("添加员工")
    public ResultResponseVo save(HttpServletRequest request, @RequestBody @Valid AddEmployeeDto addEmployeeDto) {
        return employeeService.save(request, addEmployeeDto);
    }

    /**
     * 员工信息分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工信息分页")
    public ResultResponseVo page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        return employeeService.selectByPage(page, pageSize, name);
    }

    /**
     * 修改员工状态
     *
     * @param changeStatusDto
     * @param request
     * @return
     */
    @PostMapping("/changeStatus")
    @ApiOperation("修改员工状态")
    public ResultResponseVo changeStatus(@RequestBody @Valid ChangeStatusDto changeStatusDto, HttpServletRequest request) {
        log.info("修改状态的用户为{}", changeStatusDto);
        return employeeService.changeStatus(changeStatusDto, request);
    }

    /**
     * 修改员工前获取员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("修改员工前获取员工信息")
    public ResultResponseVo getById(@PathVariable Long id) {
        log.info("修改状态的用户id为{}", id);
        return employeeService.getByEmpId(id);
    }

    /**
     * 编辑员工信息
     * @param updateEmployeeDto
     * @param request
     * @return
     */
    @PutMapping
    public ResultResponseVo update(@RequestBody @Valid UpdateEmployeeDto updateEmployeeDto, HttpServletRequest request) {
        log.info("编辑的用户信息={}", updateEmployeeDto);
        return employeeService.updateEmp(updateEmployeeDto,request);
    }

}
